<?php

class AjaxController extends Zend_Controller_Action
{
    public function init()
    {
        $this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
    }
    public function indexAction()
    {
		echo "Không có quyền truy cập !";
		$this->_redirect('index');
    }
     /**
     * admin888 <doan.hut@gmail.com>
     * Tim kiem usert bang ajax
     */ 
     public function timkiemthanhvienAction()
     {
        $timkiem = new Model_Caidat();
        $data = $this->_getAllParams();
        $out = $timkiem->gettimkiemthanhvien($data);
        if ($out){
         foreach( $out as $row){ 
            echo "<li onclick=\"viewUser('" . $row['uid'] . "');\">" . $row['name'] . "<i style=\"color:#777777;\">(" . $row['uname'] . ")</i></li>";
           }            
        } 
     }
     
     /**
      * @author : admin888 <doan.hut@gmail.com>
      * Enter description here :thực hiện chuyển trạng thái qua da sử dụng hay không sử đụng thư mẫu
      */
	public function changeuseemailAction()
	{
		$change = new Model_Thumau();
		$data = $this->_getAllParams();
		$run = $change->sudungthumau($data);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : xóa thư mẫu
	 */
	public function xoathumauAction()
	{
		$xoa = new Model_Thumau();
		$data =$this->_getAllParams();
		$delete = $xoa->deletethumau($data['idtm']);
	}
	/**
	 * admin888<doan.hut@gmail.com>
	 * Enter description :xoa toàn id post tới trong thu mẫu
	 */
	public  function xoaallthumauAction()
	{
		$xoaall = new Model_Thumau();
		$mangid = $this->_getParam('idtm');
		$xoa = $xoaall->deletealltm($mangid);
		
		
		
	}
	/**
	 * @author: admin888 
	 * Enter description :thêm nhóm liên hệ
	 */
	public function themnhomlienheAction()
	{
		$lh =new Model_Lienhe();
		$str = new stdClass();
		$data = $this->_getAllParams();
		$them = $lh->themmoinhomlienhe($data);
		//$id = (array)($them);
		$str->idlast = $them ;
		echo Zend_Json_Encoder::encode($str);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here : update nhóm lien hệ
	 */
	public function updatenhomlienheAction()
	{
		
		$lh =new Model_Lienhe();
		$data = $this->_getAllParams();
		$ud = $lh->getupdatenhomlh($data);
		$str = new stdClass();
		$str->sl = $ud;
		echo Zend_Json_Encoder::encode($str);
	}
	/**
	 * admin888
	 * Enter description here : xóa củng
	 */
	public function deletenlhAction()
	{
		$nlh = new Model_Lienhe();
		$data =$this->_getAllParams();
		$xoanhom = $nlh->deletenhomlh($data);
		$xoalienhe = $nlh->xoalienhetheonhom($data['idnlh']);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :lấy nhóm liên hệ theo iddn
	 */
	public function getallnhomlienheAction()
	{
		$nlh = new Model_Lienhe();
		$data =$this->_getAllParams();
		$dulieu = $nlh->getallnhomlhtheoiddn($data);
		//echo "<pre>";print_r($dulieu);
		
		foreach ($dulieu as $nhomlh){
			$html = "<option value='".$nhomlh['id_nlh']."'>".$nhomlh['ten_nhom_lh']."</option>";
			 echo $html;
		}
	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : ajax bat tat lien hệ
	 */
	public function battatlienheAction()
	{
		$lh = new Model_Lienhe();
		$data = $this->_getAllParams();
		$ud = $lh->getupdatelienhe($data);
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 */
	public function xoaonelienheAction()
	{
		$lh = new Model_Lienhe();
		$data = $this->_getAllParams();
		$dl = $lh->xoaonelienhe($data);
	} 
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : laod du lieu cua 1 lien he
	 */
	public function loadlienhetheoidAction()   
	{
		$lh = new Model_Lienhe();
		$data = $this->_getAllParams();
		$dulieu = $lh->getlienheid($data);
		$std = new stdClass();
		$std->dulieulayra = $dulieu;
		echo Zend_Json_Encoder::encode($std);
		//echo "<pre>"; print_r($std);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : xoa nhieu lien he 
	 */
	public function xoaalllienheAction()
	{
		$lh = new Model_Lienhe();
		$mangid = $this->_getParam('idlh');
		$xoa = $lh->deletealllh($mangid);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Tim kiếm danh bạ 
	 */
	public function timkiemlienheAction()
	{
		$lh = new Model_Lienhe();
		$data = $this->_getAllParams();
		//echo "<pre>"; print_r($data);
		if (isset($data['key']) ){
			$key = trim($data['key']);
			$dulieu = $lh->gettimkiemlienhe($data,$key);
			
			foreach ($dulieu as $tenkh){
				$html= "<li class='itemlh' onclick = \"gettenemail('".$tenkh['tenlh']."','".$tenkh['email']."','".$tenkh['idlh']."')\" > \"".$tenkh['tenlh']."\" < ".$tenkh['email']." ></li>";
				echo $html;
			}
		}
		
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : tim kiem lien hệ cho popup
	 */
	public function timkiemlienheguimailAction()
	{
		$lh = new Model_Lienhe();
		$data =$this->_getAllParams();
		$uidn = $data['iddn'];
		if (isset($data['name'])){
			$keytimkiem = trim($data['name']);
		}
		if (isset($data['idnhonlh'])){
		 	$idnhomlh = trim($data['idnhonlh']);	
		}
		//echo $keytimkiem."-".$idnhomlh;
		// thu thi
		if ($keytimkiem != 'nobody' && $idnhomlh == 0){
			// tìm theo ten 
			$dulieu = $lh->getalllienhetimkiem($uidn,$keytimkiem);
		}else if($keytimkiem == 'nobody' && $idnhomlh != 0){
			// tìm theo nhóm 
			$dulieu = $lh->getalllienhethemnhom($uidn,$idnhomlh);
		}else if($keytimkiem != 'nobody' && $idnhomlh != 0){
			// tìm theo cả tên và nhóm
			$dulieu = $lh->getalllienhethemnhomkeytim($uidn,$idnhomlh,$keytimkiem);
		}else if($keytimkiem == 'nobody' && $idnhomlh == 0){
			// lấy toàn bộ thao tác xóa bộ lọc :D
			$dulieu = $lh->getalllienhesudung($uidn);
		}
		//lặp dữ liệu
		foreach ($dulieu as $lh){
			$html=	"<tr  class=\"lienhe_pro row_".$lh['idlh']."\"> ";
			$html.=		"<td align=\"center\" >";
			$html.= 		"<input type=\"checkbox\" id=\"lhid_\" value=\"".$lh['idlh']."\" name=\"listlh[]\"/>";
			$html.=         "<input type=\"hidden\" id=\"aremail_".$lh['idlh']."\" value=\"".$lh['email']."\"/>";
			$html.=         "<input type=\"hidden\" id=\"arname_".$lh['idlh']."\" value=\"".$lh['tenlh']."\"/>";
			$html.=         "<input type=\"hidden\" id=\"aridlh_".$lh['idlh']."\" value=\"".$lh['idlh']."\"/>";
		    $html.=     "</td>";
		    $html.=     "<td > <b>".$lh['tenlh']."</b></td>";
            $html.=     "<td  align=\"center\">".$lh['email']."</td>";
			$html.=		"<td  align=\"center\">".$lh['ten_nhom_lh']."</td>";
            $html.= "</tr>";
            echo $html;
		}
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :Tìm Kiếm thư mẫu 
	 */
	public function timkiemthumauAction() {
		$tm = new Model_Thumau ();
		$data = $this->_getAllParams ();
		$iddn = $this->_getParam ( 'iddn' );
		if (isset ( $data ['idnhontm'] )) {
			$nhomtm = trim ( $data ['idnhontm'] );
		}
		if (isset ( $data ['name'] )) {
			$keytimkiem = trim ( $data ['name'] );
		}
		// thực hiện
		if ($nhomtm == 0 && $keytimkiem != 'nobody') {
			//Tìm theo tên thư
			$dulieu = $tm->getallthumautheotukhoa ( $iddn, $keytimkiem );
		} else if ($nhomtm != 0 && $keytimkiem == 'nobody') {
			//tim them laoj thu mau
			$dulieu = $tm->getallthumautheocltm($iddn,$nhomtm);
		} else if ($nhomtm != 0 && $keytimkiem != 'nobody'){
			$dulieu = $tm->getallthumautheocltmvatukhoa($iddn,$nhomtm,$keytimkiem);
		}
		foreach ( $dulieu as $tm ) {
			$html = "<tr class=\"thumau_pro row_" . $tm ['id_tm'] . "\">";
			$html .= "<td > <b>" . $tm ['ten_tm'] . "</b></td>";
			$html .= "<td  align=\"center\">" . $tm ['ngay_tao'] . "</td>";
			$html .= "<td  align=\"center\">" . $tm ['ten_loai_thu'] . "</td>";
			$html .= "<td align=\"center\" > <a href=\"#\" onclick=\"sudunthumau('" . $tm ['id_tm'] . "')\" title='Sử dụng thư mẫu này' ><b> Sử dụng </b> </a></td>";
			$html .= "</tr>";
			echo $html;
		
		}
	
	}	
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : xoa dữ lệu upload
	 */
	public function deletefileAction()
	{
		$data = $this->_getAllParams();
		//echo "<pre>"; print_r($data);die();
		$linkhinh = $data['url'].'/upload/uploads/'.$data['tenfile'];
		//echo  $linkhinh;
		$xoa = unlink($linkhinh);

	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :upload
	 */
	public function uploadfileAction()
	{
		$uploaddir = 'upload/uploads/'; 
		$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
		//$data = $this->_getAllParams();
		//echo "<pre>"; print_r($data);
		 
		if (@move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
		  echo "success"; 
		} else {
			echo "error";
		}
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : get thư mẫu cho popup
	 */
	public function getthumaupopupAction()
	{
		$tm = new Model_Thumau();
		$data = $this->_getAllParams();
		$iddn = $data['iddn'];
		$dulieu = $tm->getallthumau($iddn);
		foreach ($dulieu as $tm){
			$html=	"<tr class=\"thumau_pro row_".$tm['id_tm']."\">";
		    $html.=     "<td > <b>".$tm['ten_tm']."</b></td>";
            $html.=     "<td  align=\"center\">".$tm['ngay_tao']."</td>";
			$html.=		"<td  align=\"center\">".$tm['ten_loai_thu']."</td>";
			$html.=	    "<td align=\"center\" > <a href=\"#\" onclick=\"sudunthumau('".$tm['id_tm'].",".$tm['uid']."')\" title='Sử dụng thư mẫu này' ><b> Sử dụng </b> </a></td>";		
            $html.= "</tr>";
            echo $html;			
			
			
		}
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : get nhom thu mau theo nguoi dang nhap
	 */
	public function getgrouptmAction()
	{
		$tm = new Model_Thumau();
		$iddn = $this->_getAllParams('iddn');
		$dulieu = $tm->getdmthumau();
		foreach ($dulieu as $loathu){
			$html = "<option value=\"".$loathu['id']."\">".$loathu['ten_loai_thu']."</option>";
			echo $html;
		}
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : get thu mau 
	 */
	public function getthumausudunAction()
	{
		$tm = new Model_Thumau();
		$idthu = $this->_getParam('idtm');
		$data =  $tm->getthumau($idthu);
		$std = new stdClass();
		$std->dulieulayra = $data;
		echo Zend_Json_Encoder::encode($std);
		//echo "<pre>"; print_r($std);
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :get thu da gui 
	 */
	public function gethudaguiAction()
	{
		$gt = new Model_Thudagui(); 
		$idthu = $this->_getParam('idtdg');
		$data = $gt->getthuid($idthu);
		$srd = new stdClass();
		$srd->dulieu = $data;
		echo Zend_Json_Encoder::encode($srd);
		//echo "<pre>"; print_r($data);
	}
	/**
	 * 
	 *admin888 <doan.hut@gmail.com>
	 * Enter description :get tap tin dinh kem theo thu
	 */
	public function getdinhkemthuAction()
	{
		$gt = new Model_Thudagui(); 
		$idthu = $this->_getParam('idtdg');
		$data = $gt->getdinhkem($idthu);
		foreach ($data as $key=>$value){
			$pathinfo = pathinfo($value['link_file']);
			$data[$key]['tenfile'] = $pathinfo['basename'];
		}
		foreach ($data as $datas){
			//echo "<img class=\"hinhicondk\" src=\"".PATH_TD."/images/icon/attach_2.png\" /><a target=\"_blank\" title=\"Tải Về \" href=\"".PATH_TD."/".$datas['link_file']." \">".$datas['tenfile']." <img  src=\"".PATH_TD."/images/icon/save.png\" /></a></br> ";
			echo "<img class=\"hinhicondk\" src=\"".BASE_URL."/images/icon/attach_2.png\" /><a target=\"_blank\" title=\"Tải Về \" href=\"".BASE_URL.$datas['link_file']." \">".$datas['tenfile']." <img  src=\"".BASE_URL."images/icon/save.png\" /></a></br> ";
		}
	}
	/**
	 * Quyenvv 
	 * la danh muc theo id 
	 */
	public function getdanhmuctheoidAction()
	{
		$iddm = $this->_getParam('iddm');
		$dm = new Model_Danhmuc();
		$data = $dm->getdanhmucthu($iddm);
		$std = new stdClass();
		$std->dulieu = $data;
		echo  Zend_Json_Encoder::encode($std);
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description :update trang thai lien he
	 */	
	public function updatettlienheAction()
	{
		$data =$this->_getAllParams();
		$udlh = new Model_Lienhe();
		$up = $udlh->updatetrangthailienhe($data);
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 */
	public function gettukhoaguithuAction()
	{
		$tukhoa = new Model_Tukhoa();
		$data   = $tukhoa->getalltukhoa();
		//echo "<pre>"; print_r($data);
		foreach ($data as $tukhoa){
		echo "<tr align=\"centrer\">";
		echo 	"<td ><b>".$tukhoa['ten_tu_khoa']."</b></td>";
		echo 	"<td >".$tukhoa['mo_ta']."</td>";
		echo 	"<td align=\"center\" > <a href=\"#\" onclick=\"sudungtukhoa('".$tukhoa['ten_tu_khoa']."','".$tukhoa['tk_id']."')\" >Sử dụng</a></td>";
		echo  "</tr>";
		}
	}
	/**
	 * admin888 <pavent@gmail.com>
	 * Enter description : get tu khoa
	 */
	public function gettukhoacansuaAction()
	{
		$idtukhoa = $this->_getParam('idtk');
		$tk = new Model_Tukhoa();
		$dulieu = $tk->gettukhoatheoid($idtukhoa);
		$tukhoadacat=   str_replace(array('{','}'), '', $dulieu['ten_tu_khoa']);
		foreach ($dulieu as $key => $value){
			$dulieu['keypaven'] = $tukhoadacat;
		}
		$std = new stdClass();
		$std->dulieu = $dulieu;
		echo Zend_Json_Encoder::encode($std);
	}
	
	/**
	 * dức 
	 * Enter description : xóa thanh vien
	 */
	public function xoathanhvienAction()
	{
		$xoa = new Model_Caidat();
		$data =$this->_getAllParams();
		
		
	    //xoa fie tron thu muc 
		$arrlinkfile = $xoa->getalllinkfile($data['idtv']);
		
		foreach ($arrlinkfile as $lik){
			$xoafilethumuc = $xoa->xoafilecuathumuc($lik['link_file']);
		}
		$delete = $xoa->deletethanhvien($data['idtv']);
	}
	/**
	 * admin888<doan.hut@gmail.com>
	 * Enter description :xoa toàn id post tới trong thanh vien
	 */
	public  function xoaallthanhvienAction()
	{
		$xoaall = new Model_Caidat();
		$mangid = $this->_getParam('idtv');
		//xoa fie tron thu muc
	   $arrlinkfile = $xoaall->getalllinkfile($mangid);
		
		foreach ($arrlinkfile as $lik){
			$xoafilethumuc = $xoaall->xoafilecuathumuc($lik['link_file']);
		}
		$xoa = $xoaall->deletealltv($mangid);
		
	}
	
}
?>